# This scenario re-creates figure 8 from the Raft paper

start_node,0
assert_detail,0,leadership_state,Leader
emit_signature,2

assert_detail,0,leadership_state,Leader
assert_commit_idx,0,2

trust_node,2,1
emit_signature,2

dispatch_all
periodic_all,10
dispatch_all
assert_commit_idx,0,4

trust_node,2,2
emit_signature,2

dispatch_all
periodic_all,10
dispatch_all
assert_commit_idx,0,6

trust_node,2,3
emit_signature,2

dispatch_all
periodic_all,10
dispatch_all
assert_commit_idx,0,8

trust_node,2,4
emit_signature,2

dispatch_all
periodic_all,10
dispatch_all
periodic_all,10
dispatch_all
assert_commit_idx,0,10
assert_state_sync

# Node 0 appends a single entry, replicates it to all nodes
replicate,2,entry_1
emit_signature,2

periodic_all,10
dispatch_all

# Node 0 appends a second entry, which only reaches Node 1
disconnect_node,0
connect,0,1
replicate,2,entry_2
emit_signature,2

periodic_all,10
dispatch_all

# Node 4 becomes primary, appends a single entry which reaches nobody
disconnect_node,0
connect,4,3
connect,4,2

periodic_one,4,110
dispatch_all

replicate,3,entry_3
emit_signature,3

state_all

# Node 0 returns, becomes primary again, manages to share second entry with additional node
disconnect_node,4
connect,0,1
connect,0,2

periodic_all,10
dispatch_all

periodic_one,0,110
dispatch_all

periodic_all,10
dispatch_all # NB: 1.2 is now present on a majority of nodes, and node 0 knows this

# Node 0 now loses connectivity, Node 4 wins election in a new term
disconnect_node,0
connect,4,3
connect,4,2
connect,4,1

periodic_all,10
dispatch_all

periodic_one,4,110
dispatch_all

periodic_all,10
dispatch_all # NB: Nodes 1 and 2 have discarded their state, which a majority held
state_all

periodic_all,10
dispatch_all

# All nodes become connected - can they reach eventual agreement?
connect,0,4

periodic_all,10
dispatch_all

periodic_all,10
dispatch_all

periodic_all,10
dispatch_all

state_all

# Not yet! We need node 4 to produce a new entry in its current term
replicate,5,entry_4
emit_signature,5

periodic_all,10
dispatch_all

periodic_all,10
dispatch_all

state_all
assert_state_sync
